/* 
 * Copyright (C) 2001-2003 Jacek Sieka, j_s@telia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "FavoritesFrm.h"
#include "HubFrame.h"
#include "FavHubProperties.h"

#include "../client/ClientManager.h"
#include "../client/StringTokenizer.h"
// CDM EXTENSION BEGINS
//Add to favs
#include "../client/ClientManager.h"
int FavoriteHubsFrame::columnIndexes[] = { COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_NICK, COLUMN_PASSWORD, COLUMN_SERVER, COLUMN_USERDESCRIPTION 
, COLUMN_C_CLIENT, COLUMN_C_FILELIST, COLUMN_C_FAKESHARE, COLUMN_C_ONCONNECT, COLUMN_SEND_UIP, COLUMN_HIDE_SHARE, 
//Sulan start 28
COLUMN_TRIGGER,
//Sulan start svn86
COLUMN_LOG_CHAT,
// pothead starts
COLUMN_USE_MYINFO_DETECT, COLUMN_LAST_STATUS_AT_LOGON,
// pothead ends
};
int FavoriteHubsFrame::columnSizes[] = { 180, 250, 110, 90, 75, 75
,75 ,75 ,75, 75, 75, 75
//Sulan start 28
, 50
//Sulan start svn86
, 75
// pothead starts
, 75, 75
// pothead ends
};
static ResourceManager::Strings columnNames[] = { ResourceManager::AUTO_CONNECT, ResourceManager::DESCRIPTION, ResourceManager::NICK, 
ResourceManager::PASSWORD, ResourceManager::SERVER, ResourceManager::USER_DESCRIPTION, 
ResourceManager::C_CLIENT, ResourceManager::C_FILELIST, ResourceManager::C_FAKESHARE, ResourceManager::C_ONCONNECT, ResourceManager::SEND_UIP, ResourceManager::HIDE_SHARE
//Sulan start 28
, ResourceManager::TRIGGER
//Sulan start svn86
, ResourceManager::SETTINGS_LOG_MAIN_CHAT
// pothead starts
, ResourceManager::MYINFO_DETECT, ResourceManager::LAST_LOGON_STATUS
// pothead ends
};
// CDM EXTENSION ENDS
LRESULT FavoriteHubsFrame::onCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled) {
	ctrlHubs.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 
		WS_HSCROLL | WS_VSCROLL | LVS_REPORT | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER, WS_EX_CLIENTEDGE, IDC_HUBLIST);

	if(BOOLSETTING(FULL_ROW_SELECT)) {
		ctrlHubs.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES | LVS_EX_HEADERDRAGDROP);
	} else {
		ctrlHubs.SetExtendedListViewStyle(LVS_EX_CHECKBOXES | LVS_EX_HEADERDRAGDROP);
	}
	
	ctrlHubs.SetBkColor(WinUtil::bgColor);
	ctrlHubs.SetTextBkColor(WinUtil::bgColor);
	ctrlHubs.SetTextColor(WinUtil::textColor);
	
	// Create listview columns
	WinUtil::splitTokens(columnIndexes, SETTING(FAVORITESFRAME_ORDER), COLUMN_LAST);
	WinUtil::splitTokens(columnSizes, SETTING(FAVORITESFRAME_WIDTHS), COLUMN_LAST);
	
	for(int j=0; j<COLUMN_LAST; j++) {
		int fmt = LVCFMT_LEFT;
		ctrlHubs.InsertColumn(j, CTSTRING_I(columnNames[j]), fmt, columnSizes[j], j);
	}
	
	ctrlHubs.SetColumnOrderArray(COLUMN_LAST, columnIndexes);
	
	ctrlConnect.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
		BS_PUSHBUTTON , 0, IDC_CONNECT);
	ctrlConnect.SetWindowText(CTSTRING(CONNECT));
	ctrlConnect.SetFont(WinUtil::font);

	ctrlNew.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
		BS_PUSHBUTTON , 0, IDC_NEWFAV);
	ctrlNew.SetWindowText(CTSTRING(NEW));
	ctrlNew.SetFont(WinUtil::font);

	ctrlProps.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
		BS_PUSHBUTTON , 0, IDC_EDIT);
	ctrlProps.SetWindowText(CTSTRING(PROPERTIES));
	ctrlProps.SetFont(WinUtil::font);

	ctrlRemove.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
		BS_PUSHBUTTON , 0, IDC_REMOVE);
	ctrlRemove.SetWindowText(CTSTRING(REMOVE));
	ctrlRemove.SetFont(WinUtil::font);

	ctrlUp.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
		BS_PUSHBUTTON , 0, IDC_MOVE_UP);
	ctrlUp.SetWindowText(CTSTRING(MOVE_UP));
	ctrlUp.SetFont(WinUtil::font);

	ctrlDown.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
		BS_PUSHBUTTON , 0, IDC_MOVE_DOWN);
	ctrlDown.SetWindowText(CTSTRING(MOVE_DOWN));
	ctrlDown.SetFont(WinUtil::font);

	HubManager::getInstance()->addListener(this);
	updateList(HubManager::getInstance()->getFavoriteHubs());
	
	hubsMenu.CreatePopupMenu();
	hubsMenu.AppendMenu(MF_STRING, IDC_CONNECT, CTSTRING(CONNECT));
	hubsMenu.AppendMenu(MF_STRING, IDC_NEWFAV, CTSTRING(NEW));
	hubsMenu.AppendMenu(MF_STRING, IDC_EDIT, CTSTRING(PROPERTIES));
	hubsMenu.AppendMenu(MF_STRING, IDC_MOVE_UP, CTSTRING(MOVE_UP));
	hubsMenu.AppendMenu(MF_STRING, IDC_MOVE_DOWN, CTSTRING(MOVE_DOWN));
	hubsMenu.AppendMenu(MF_SEPARATOR);
	hubsMenu.AppendMenu(MF_STRING, IDC_REMOVE, CTSTRING(REMOVE));
	hubsMenu.SetMenuDefaultItem(IDC_CONNECT);

	nosave = false;

	m_hMenu = WinUtil::mainMenu;

	bHandled = FALSE;
	return TRUE;
}

void FavoriteHubsFrame::openSelected() {
	if(!checkNick())
		return;

	int i = -1;
	while( (i = ctrlHubs.GetNextItem(i, LVNI_SELECTED)) != -1) {
		FavoriteHubEntry* entry = (FavoriteHubEntry*)ctrlHubs.GetItemData(i);
		HubFrame::openWindow(entry->getServer(), entry->getNick(), entry->getPassword(), entry->getUserDescription()
			// CDM EXTENSION BEGINS FAVS
			//Add to favs
			, entry->getRawOne()
			, entry->getRawTwo()
			, entry->getRawThree()
			, entry->getRawFour()
			, entry->getRawFive()
			, entry->getRawSix()
			, entry->getRawSeven()
			, entry->getRawEight()
			, entry->getRawNine()
			, entry->getRawTen()
			, entry->getOpChat()
			, entry->getUserIp()
			, entry->getCheckClients()
			, entry->getCheckFilelists()
			, entry->getCheckFakeShare()
			, entry->getCheckOnConnect()
			//Gabberworld start
			, entry->getCountryCodeCheck()
			, entry->getCountryCodeRaw()
			//Sulan start
			, entry->getProtectedUsers()
			, entry->getHideShare()
			//Sulan start 28
			, entry->getTrigger()
			//Sulan start svn86
			, entry->getLogChat()
			//Sulan start svn101
			, entry->getColumnOrder()
			, entry->getColumnWidth()
			, entry->getColumnVisible()
			//Sulan start svn124
			, 
			entry->getWindowPosX(), 
			entry->getWindowPosY(), 
			entry->getWindowSizeX(), 
			entry->getWindowSizeY(), 
			entry->getWindowType()
			//End
			// pothead starts
			, entry->getHubMessage()
			, entry->getUseMyinfoDetect()
			, entry->getRegStatus()
			, entry->getDisableRaws()
			// pothead ends
			// CDM EXTENSION ENDS
			);
	}
	return;
}

LRESULT FavoriteHubsFrame::onDoubleClickHublist(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) {
	if(!checkNick())
		return 0;
	
	NMITEMACTIVATE* item = (NMITEMACTIVATE*) pnmh;

	if(item->iItem != -1) {
		FavoriteHubEntry* entry = (FavoriteHubEntry*)ctrlHubs.GetItemData(item->iItem);
		HubFrame::openWindow(entry->getServer(), entry->getNick(), entry->getPassword(), entry->getUserDescription()
			// CDM EXTENSION BEGINS FAVS
			//Add to favs
			, entry->getRawOne()
			, entry->getRawTwo()
			, entry->getRawThree()
			, entry->getRawFour()
			, entry->getRawFive()
			, entry->getRawSix()
			, entry->getRawSeven()
			, entry->getRawEight()
			, entry->getRawNine()
			, entry->getRawTen()
			, entry->getOpChat()
			, entry->getUserIp()
			, entry->getCheckClients()
			, entry->getCheckFilelists()
			, entry->getCheckFakeShare()
			, entry->getCheckOnConnect()
			//Gabberworld start
			, entry->getCountryCodeCheck()
			, entry->getCountryCodeRaw()
			//Sulan start
			, entry->getProtectedUsers()
			, entry->getHideShare()
			//Sulan start 28
			, entry->getTrigger()
			//Sulan start svn86
			, entry->getLogChat()
			//Sulan start svn101
			, entry->getColumnOrder()
			, entry->getColumnWidth()
			, entry->getColumnVisible()
			//Sulan start svn124
			, 
			entry->getWindowPosX(), 
			entry->getWindowPosY(), 
			entry->getWindowSizeX(), 
			entry->getWindowSizeY(), 
			entry->getWindowType()
			//End
			// pothead starts
			, entry->getHubMessage()
			, entry->getUseMyinfoDetect()
			, entry->getRegStatus()
			, entry->getDisableRaws()
			// pothead ends
			// CDM EXTENSION ENDS
			);
	}

	return 0;
}

LRESULT FavoriteHubsFrame::onRemove(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(MessageBox(_T("Are you sure you want to remove the selected hub(s)?"), _T("Remove?"), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES) {
		int i = -1;
		while( (i = ctrlHubs.GetNextItem(-1, LVNI_SELECTED)) != -1) {
			HubManager::getInstance()->removeFavorite((FavoriteHubEntry*)ctrlHubs.GetItemData(i));
		}
	}
	return 0;
}

LRESULT FavoriteHubsFrame::onEdit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	int i = -1;
	if((i = ctrlHubs.GetNextItem(i, LVNI_SELECTED)) != -1)
	{
		FavoriteHubEntry* e = (FavoriteHubEntry*)ctrlHubs.GetItemData(i);
		dcassert(e != NULL);
		FavHubProperties dlg(e);
		if(dlg.DoModal(m_hWnd) == IDOK)
		{
			ctrlHubs.SetItemText(i, COLUMN_NAME, e->getName().c_str());
			ctrlHubs.SetItemText(i, COLUMN_DESCRIPTION, e->getDescription().c_str());
			ctrlHubs.SetItemText(i, COLUMN_SERVER, e->getServer().c_str());
			ctrlHubs.SetItemText(i, COLUMN_NICK, e->getNick(false).c_str());
			ctrlHubs.SetItemText(i, COLUMN_PASSWORD, string(e->getPassword().size(), '*').c_str());
			ctrlHubs.SetItemText(i, COLUMN_USERDESCRIPTION, e->getUserDescription().c_str());
			// CDM EXTENSION BEGINS FAVS
			//Add to favs
			//Sulan
			ctrlHubs.SetItemText(i, COLUMN_C_CLIENT, WinUtil::toYesNo(e->getCheckClients()).c_str());
			ctrlHubs.SetItemText(i, COLUMN_C_FILELIST, WinUtil::toYesNo(e->getCheckFilelists()).c_str());
			ctrlHubs.SetItemText(i, COLUMN_C_FAKESHARE, WinUtil::toYesNo(e->getCheckFakeShare()).c_str());
			ctrlHubs.SetItemText(i, COLUMN_C_ONCONNECT, WinUtil::toYesNo(e->getCheckOnConnect()).c_str());
			ctrlHubs.SetItemText(i, COLUMN_SEND_UIP, WinUtil::toYesNo(e->getUserIp()).c_str());
			ctrlHubs.SetItemText(i, COLUMN_HIDE_SHARE, WinUtil::toYesNo(e->getHideShare()).c_str());
			//Sulan start 28
			ctrlHubs.SetItemText(i, COLUMN_TRIGGER, e->getTrigger().c_str());
			//Sulan start svn86
			ctrlHubs.SetItemText(i, COLUMN_LOG_CHAT, WinUtil::toYesNo(e->getLogChat()).c_str());
			//End
			// pothead starts
			ctrlHubs.SetItemText(i, COLUMN_USE_MYINFO_DETECT, WinUtil::toYesNo(e->getUseMyinfoDetect()).c_str());
			ClientManager::getInstance()->reloadFavHubs();
			// CDM EXTENSION ENDS FAVS
		}
	}
	return 0;
}

LRESULT FavoriteHubsFrame::onNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	FavoriteHubEntry e;
	FavHubProperties dlg(&e);

	if(dlg.DoModal((HWND)*this) == IDOK)
		HubManager::getInstance()->addFavorite(e);
	return 0;
}

bool FavoriteHubsFrame::checkNick() {
	if(SETTING(NICK).empty()) {
		MessageBox(CTSTRING(ENTER_NICK), _T(APPNAME) _T(" ") _T(VERSIONSTRING), MB_ICONSTOP | MB_OK);
		return false;
	}
	return true;
}

LRESULT FavoriteHubsFrame::onMoveUp(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	nosave = true;
	int j = ctrlHubs.GetItemCount();
	FavoriteHubEntry::List& fh = HubManager::getInstance()->getFavoriteHubs();
	ctrlHubs.SetRedraw(FALSE);
	for(int i = 1; i < j; ++i) {
		if(ctrlHubs.GetItemState(i, LVIS_SELECTED)) {
			FavoriteHubEntry* e = fh[i];
			swap(fh[i], fh[i-1]);
			ctrlHubs.DeleteItem(i);
			addEntry(e, i-1);
			ctrlHubs.SetItemState(i-1, LVIS_SELECTED, LVIS_SELECTED);
			ctrlHubs.EnsureVisible(i-1, FALSE);
		}
	}
	ctrlHubs.SetRedraw(TRUE);
	nosave = false;
	HubManager::getInstance()->save();
	return 0;
}

LRESULT FavoriteHubsFrame::onMoveDown(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	int j = ctrlHubs.GetItemCount() - 2;
	FavoriteHubEntry::List& fh = HubManager::getInstance()->getFavoriteHubs();

	nosave = true;
	ctrlHubs.SetRedraw(FALSE);
	for(int i = j; i >= 0; --i) {
		if(ctrlHubs.GetItemState(i, LVIS_SELECTED)) {
			FavoriteHubEntry* e = fh[i];
			swap(fh[i], fh[i+1]);
			addEntry(e, i+2);
			ctrlHubs.SetItemState(i+2, LVIS_SELECTED, LVIS_SELECTED);
			ctrlHubs.DeleteItem(i);
			ctrlHubs.EnsureVisible(i, FALSE);
		}
	}
	ctrlHubs.SetRedraw(TRUE);
	nosave = false;
	HubManager::getInstance()->save();
	return 0;
}

LRESULT FavoriteHubsFrame::onItemChanged(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) {
	NMITEMACTIVATE* l = (NMITEMACTIVATE*)pnmh;
	if(!nosave && l->iItem != -1 && ((l->uNewState & LVIS_STATEIMAGEMASK) != (l->uOldState & LVIS_STATEIMAGEMASK))) {
		FavoriteHubEntry* f = (FavoriteHubEntry*)ctrlHubs.GetItemData(l->iItem);
		f->setConnect(ctrlHubs.GetCheckState(l->iItem) != FALSE);
		HubManager::getInstance()->save();
	}
	return 0;
}

LRESULT FavoriteHubsFrame::onClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled) {
	HubManager::getInstance()->removeListener(this);
	
	WinUtil::saveHeaderOrder(ctrlHubs, SettingsManager::FAVORITESFRAME_ORDER, 
		SettingsManager::FAVORITESFRAME_WIDTHS, COLUMN_LAST, columnIndexes, columnSizes);
	// CDM EXTENSION BEGINS
	//Sulan
	ClientManager::getInstance()->reloadFavHubs();
	// CDM EXTENSION ENDS
	bHandled = FALSE;
	return 0;
}

void FavoriteHubsFrame::UpdateLayout(BOOL bResizeBars /* = TRUE */)
{
	RECT rect;
	GetClientRect(&rect);
	// position bars and offset their dimensions
	UpdateBarsPosition(rect, bResizeBars);

	CRect rc = rect;
	rc.bottom -=28;
	ctrlHubs.MoveWindow(rc);

	const long bwidth = 90;
	const long bspace = 10;

	rc = rect;
	rc.bottom -= 2;
	rc.top = rc.bottom - 22;

	rc.left = 2;
	rc.right = rc.left + bwidth;
	ctrlNew.MoveWindow(rc);

	rc.OffsetRect(bwidth+2, 0);
	ctrlProps.MoveWindow(rc);

	rc.OffsetRect(bwidth+2, 0);
	ctrlRemove.MoveWindow(rc);

	rc.OffsetRect(bspace + bwidth +2, 0);
	ctrlUp.MoveWindow(rc);

	rc.OffsetRect(bwidth+2, 0);
	ctrlDown.MoveWindow(rc);

	rc.OffsetRect(bspace + bwidth +2, 0);
	ctrlConnect.MoveWindow(rc);

}

void FavoriteHubsFrame::on(HubManagerListener::Types type, FavoriteHubEntry* entry) throw() {
	switch(type) {
		case HubManagerListener::FAVORITE_ADDED: addEntry(entry, ctrlHubs.GetItemCount()); break;
		case HubManagerListener::FAVORITE_REMOVED: ctrlHubs.DeleteItem(ctrlHubs.find((LPARAM)entry)); break;
	}
};
/**
 * @file
 * $Id: FavoritesFrm.cpp 329 2005-08-30 21:06:02Z Pothead $
 */